
/****** Object:  StoredProcedure [dbo].[ws_ins_voucher]    Script Date: 10/19/2013 09:22:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'ws_ins_voucher_n')
DROP PROCEDURE ws_ins_voucher_n
GO

CREATE PROCEDURE [dbo].[ws_ins_voucher_n] 
(		
	@aparato_id varchar(255), 
	@kortelesnr varchar(255), 
	@barkodas varchar(255), 
	@kiekis varchar(255), 
	@suma varchar(255),
	@pvm varchar(255)
)
AS
BEGIN
	DECLARE @zetas int
	DECLARE @nuo datetime
	DECLARE @iki datetime
	DECLARE @galvaid int
	DECLARE @data datetime
	DECLARE @kvitonr int
	DECLARE @KvNr int
	DECLARE @tmp varchar(11)
	DECLARE @mok numeric(12,2)
	

	BEGIN TRANSACTION
		SET @data = current_timestamp	
	
		insert into Kvitogalva_n  (aparatoid,kortelesnr,data,import,year,month,day,hour,minute,suma,apmokejimorusis) 
		values (@aparato_id,@kortelesnr,@data,0,year(@data),month(@data),day(@data), DATEPART(hh, @data),DATEPART(mi,@data),@suma,1)
		
		SET @galvaid  = SCOPE_IDENTITY()

		SET @mok = cast(@suma as numeric(12,2))-(cast(@suma as numeric(12,2))*100/(100+cast(@pvm as numeric(10,2)))) 
		
		insert into Kvitoeilute_n (barkodas,kiekis,galvosid,prekeskodas,suma,mokesciai ) 
		values (@barkodas,@kiekis,@galvaid,@barkodas,@suma,@mok)
		
	COMMIT TRANSACTION

END

GO

